package com.example.dialog_demo.slice;

import com.example.dialog_demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Text;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.*;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;

public class MainAbilitySlice extends AbilitySlice {

    private static final int DIALOG_BOX_WIDTH = 948;
    private Button toastButton;
    private Button listButton;
    private Button commonButton;
    private Button displayButton;
    private Button popupButton;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initComponent();
    }

    private void initComponent() {
        toastButton = (Button) findComponentById(ResourceTable.Id_btn_toastdialog);
        toastButton.setClickedListener(v -> {
            showToastDialog();
        });
        listButton = (Button) findComponentById(ResourceTable.Id_btn_listdialog);
        listButton.setClickedListener(v -> {
            showListDialog();
        });
        commonButton = (Button) findComponentById(ResourceTable.Id_btn_commondialog);
        commonButton.setClickedListener(v -> {
            showCommonDialog();
        });
        displayButton = (Button) findComponentById(ResourceTable.Id_btn_displaydialog);
        displayButton.setClickedListener(v -> {
            showDisplayDialog();
        });
        popupButton = (Button) findComponentById(ResourceTable.Id_btn_popupdialog);
        popupButton.setClickedListener(v -> {
            showPopupButton();
        });

    }

    private void showListDialog() {
        ListDialog listDialog = new ListDialog(this);
        String[] items = new String[]{"item 1", "item 2", "item 3"};
        listDialog.setAlignment(TextAlignment.CENTER);
        listDialog.setSize(DIALOG_BOX_WIDTH, MATCH_CONTENT);
        listDialog.setTitleText("This Is List Dialog");
        listDialog.setAutoClosable(true);
        listDialog.setItems(items);
        listDialog.setOnSingleSelectListener((iDialog, index) -> {
           System.out.println(index);
            iDialog.destroy();
        });
        listDialog.show();
    }

    private void showToastDialog() {

        ToastDialog toastDialog = new ToastDialog(MainAbilitySlice.this);
        toastDialog.setText("这是ToastDialog")
                .setSize(DIALOG_BOX_WIDTH, MATCH_CONTENT)
                .setAutoClosable(true)
                .setDuration(2000)
                .setAlignment(0)
                .show();

    }

    private void showCommonDialog() {
        CommonDialog commonDialog = new CommonDialog(this);
        commonDialog.setTitleText("This Is Common Dialog");
        commonDialog.setContentText("Hello common dialog");
        commonDialog.setCornerRadius(50);
        commonDialog.setAlignment(TextAlignment.CENTER);
        commonDialog.setSize(DIALOG_BOX_WIDTH, MATCH_CONTENT);
        commonDialog.setAutoClosable(true);
        commonDialog.setButton(IDialog.BUTTON1, "是", (iDialog, var) -> {
//            resultText.setText("You Clicked Yes Button");
            iDialog.destroy();
        });
        commonDialog.setButton(IDialog.BUTTON2, "否", (iDialog, var) -> {
//            resultText.setText("You Clicked No Button");
            iDialog.destroy();
        });
        commonDialog.show();
    }




    private void showPopupButton() {
        PopupDialog popupDialog = new PopupDialog(this, new Text(this));
        popupDialog.show();

    }

    private void showDisplayDialog() {
        DisplayDialog displayDialog = new DisplayDialog(this);
        displayDialog.show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
